/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.anhndt.audiobook.ui;

import com.anhndt.audiobook.ui.renderer.ItemArticlePartRenderer;
import com.anhndt.audiobook.ui.renderer.ItemRenderer;
import com.anhndt.audiobook.business.Resource;
import com.anhndt.audiobook.entity.ItemEntity;
import com.anhndt.audiobook.iface.ProcessListener;
import com.anhndt.audiobook.utils.StringUtil;
import com.anhndt.audiobook.utils.Util;
import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Graphics;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.FocusListener;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.list.ListCellRenderer;
import java.util.Vector;

/**
 *
 * @author Sun
 */
public class ArticlePartForm extends ArticleForm {

    private ItemEntity entityArticle;
    ItemArticleRenderer itemArticle;
    public ArticlePartForm(String title, BaseForm previous, ItemEntity parentEntity, ItemEntity entityArticle) {
        super(title, previous, parentEntity);
        this.entityArticle = entityArticle;
        if(this.entityArticle == null)
            this.entityArticle = parentEntity;
    }

    protected String getTypeContentChild() {
        return "" + ItemEntity.TYPE_ARTICLE_PART;
    }

    protected ListCellRenderer getRenderer() {
        return new ItemArticlePartRenderer();
    }

    protected void init() {
        super.init();
        //add entity article
        //alway show third button
        ItemEntity tmp = getArticleEntity();
        if (entityArticle == null || !entityArticle.equals(tmp)) {
            entityArticle = tmp;
        }
        if (entityArticle != null) {
            itemArticle = new ItemArticleRenderer(entityArticle);
            itemArticle.getListCellRendererComponent(null, entityArticle, 1, false);
            body.addComponent(BorderLayout.NORTH, itemArticle);
            loadThumbArticle();
        }
        form.addCommand(new Command(Resource.CMD_DOWNLOAD) {

            public void actionPerformed(ActionEvent evt) {
                if (form.getFocused() == listItem) {
                    ItemEntity entity = (ItemEntity) listItem.getSelectedItem();
                    download(entity);
                }
            }
        });
    }

    public static void download(final ItemEntity entity) {
        Thread t = new Thread() {

            public void run() {
//                String url = "http://192.168.1.148:81/audiows/audiofile.aspx?u=1&a=6&p=224";
                String url = StringUtil.format(Util.AUDIO_FILE_URL,
                    new String[]{Util.userID, entity.getParent().id, entity.id});
                AudioStore.execute(url);
            }
        };
        t.start();
    }

    protected void itemSelected() {
        listItem.setHandlesInput(true);
        selectedIndex = listItem.getSelectedIndex();
        formChild = new MediaPlayerForm(title, this, parentEntity, listItem.getSelectedIndex());
        showFormChild(formChild);
    }

    private void loadThumbArticle() {
        if (entityArticle.hasThumbnail() && entityArticle.icon == null) {
            Vector vt = new Vector();
            vt.addElement(entityArticle);
            ProcessListener callback = new ProcessListener() {

                public void onSuccess(Object result, int index) {
                    itemArticle.getListCellRendererComponent(null, entityArticle, 1, false);
                }

                public void onError(String status, int index) {
                }
            };
            loadThumb(vt, Util.sizeThumb, Util.sizeThumb, getPrefixLoadThumb(), true, callback);
        }
    }

    private ItemEntity getArticleEntity() {
        Vector vt = parentEntity.getItemChildrends("" + ItemEntity.TYPE_ARTICLE);
        return vt.size() > 0 ? (ItemEntity) vt.elementAt(0) : null;
    }

    class ItemArticleRenderer extends ItemRenderer {

        public ItemArticleRenderer(final ItemEntity article) {
            super(ItemRenderer.TYPE_RATE);
            setFocusable(false);
            rate.setFocusable(true);
            rate.addFocusListener(new FocusListener() {

                public void focusGained(Component cmp) {
                }

                public void focusLost(Component cmp) {
                    //rating
                    if (rate.getFocusRate() != rate.getRate()) {
                        rate.callServiceRate(article.id);
                    }
                }
            });
        }

        protected void paintBackground(Graphics g) {
//            super.paintBackground(g);
        }

        public void paintBackgrounds(Graphics g) {
//            super.paintBackgrounds(g);
        }
    }
}
